package com.chance.cc.crawler.prod.command.job.domain.news.tenxun.qqcarnews;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.tenxun.TXCommonScript;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class QqCarNewsCrawlerScheduleJob extends TXCommonScript {
    public static void main(String[] args) {
        publishQqcarnewsArticleScheduleJobInfo();
    }
    public static CrawlerJob publishQqcarnewsArticleScheduleJobInfo(){
        CrawlerJob crawlerJob = articleSchedule();

        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();//采集作业
        crawlerScheduleJob.setDomain(domain);//设置域
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));//设置采集信息
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());//工作标签
        crawlerScheduleJob.setNote("腾讯汽车采集文章回复评论");//采集提示
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());//生成爬虫key
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);//发布标签命令添加和修改调度工作
        System.out.println("发布作业："+httpPage.getRawText());//打印文本
        return crawlerJob;//返回工作爬虫
    }
    public static CrawlerJob articleSchedule(){
        String site = "car";
        String site_biz = "realtime";
        String module = "newcar";
        String url = "https://auto.qq.com/"+module+".htm";
        CrawlerRequestRecord requestRecord =  CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                //.filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);

        List<String> moduleTags = new ArrayList<>();//按照不同的功能模块区分不同站点
        //moduleTags.add("guide");
        //moduleTags.add("evaluat");
        //moduleTags.add("tech");
        //moduleTags.add("news");
        requestRecord.tagsCreator().bizTags().addCustomKV("moduleTags",moduleTags);

        String requestQueueName = StringUtils.joinWith("-", "crawler", domain, site, site_biz, "queue");
        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        //tenxun-car-realtime-realtime  job key
                        StringUtils.joinWith("-",site, site_biz, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime)
                )
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                //.fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/gome.log",false)
                .kafkaResultPipeline(null, kafkaTopic, null)
                .requestRecord(requestRecord)
                .build();
        return crawlerJob;
    }
}
